package com.yand.redis;


import redis.clients.jedis.Jedis;

import java.util.Set;
import java.util.concurrent.*;

/**
 * 用redis 的list 模拟消息队列进行 秒杀的处理
 */
public class SeckillTest {

    public static void main(String  [] args){
        long starTime=System.currentTimeMillis();

        initPrduct();
        initClient();
        printResult();

        long endTime=System.currentTimeMillis();
        long Time=endTime-starTime;
        System.out.println("程序运行时间： "+Time+"ms");
    }

    private static void printResult() {
        Jedis jedis = RedisUtil.getJedis();
        Set<String> set = jedis.smembers("clientList");

        int i = 1;
        for (String value : set) {
            System.out.println("第" + i++ + "个抢到商品，"+value + " ");
        }

        RedisUtil.returnResource(jedis);
    }




    private static void initClient() {
        ExecutorService execs =  new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                    60L, TimeUnit.SECONDS,
                                    new SynchronousQueue<Runnable>());



        int clientNum = 1000;// 模拟客户数目
        CountDownLatch count = new CountDownLatch(clientNum);
        for (int i = 0; i < clientNum; i++) {
            execs.execute(new ClientThreadNew(i, count));
        }
        try {
            count.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        execs.shutdown();

        /*while(true){
            if(execs.isTerminated()){
                System.out.println("所有的线程都结束了！");
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }*/

    }


    /**
     * 这边进行 初始化商品操作
     */
    public static void initPrduct(){
        int prdNum = 10;// 商品个数
        String key = "prdNum";
        String clientList = "clientList";// 抢购到商品的顾客列表



        Jedis jedis = RedisUtil.getJedis();

        if(jedis.exists(clientList)){
            jedis.del(clientList);
        }

        if(jedis.exists(key)){
            jedis.del(key);
        }

        for(int i = 0 ; i < prdNum; i ++){

            jedis.lpush(key, i+"");
        }

    }




}


class ClientThreadNew implements Runnable{
    Jedis jedis = null;
    String key = "prdNum";// 商品主键
    String clientList = "clientList";//// 抢购到商品的顾客列表主键
    String clientName;

    CountDownLatch count;

    public ClientThreadNew(int num, CountDownLatch count) {
        clientName = "编号=" + num;
        this.count = count;
    }

    @Override
    public void run() {
        try {
            Thread.sleep((int)(Math.random()*1000));// 随机睡眠一下
        } catch (InterruptedException e1) {
        }

        System.out.println("顾客:" + clientName + "开始抢商品");
        jedis = RedisUtil.getJedis();
        Long num = jedis.llen(key);
        if(num > 0){
            String flag = jedis.rpop(key);
            if(flag != null){
                jedis.sadd(clientList, clientName);// 抢到商品记录一下
                System.out.println("好高兴，顾客:" + clientName + "抢到商品");
            }else{
                System.out.println("悲剧了，顾客:" + clientName + "没有抢到商品");
            }
        }else{
            System.out.println("悲剧了，库存为0，顾客:" + clientName + "没有抢到商品");
        }


        RedisUtil.returnResource(jedis);
        count.countDown();



    }
}